package com.fsnbigdata.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 * @author cqz
 * @title: DateCuteUtil
 * @description: TODO
 * @date 2019/4/2615:02
 */
public class DateCuteUtil {

    /**
     * 获取某天的时间
     * index 为正表示当前时间加天数，为负表示当前时间减天数
     * @param index
     * @return String
     */
    public static Date getTimeByWeek(Calendar calendars,int index){
        TimeZone tz = TimeZone.getTimeZone("Asia/Shanghai");
        TimeZone.setDefault(tz);
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar = calendars;
        // 增加天数
        calendar.add(Calendar.DAY_OF_MONTH,index);
        String dates = fmt.format(calendar.getTime());
        System.out.println("当前日期"+index+"天的时间："+dates);
        Date date = calendar.getTime();
        return date;
    }

    /**
     * 月
     * @param index
     * @return
     */
    public static Date getTimeByMonth(Calendar calendars,int index){
        TimeZone tz = TimeZone.getTimeZone("Asia/Shanghai");
        TimeZone.setDefault(tz);
        Calendar calendar = calendars;
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        calendar.add(Calendar.MONTH,index);
        String m_date = fmt.format(calendar.getTime());
        System.out.println("当前日期"+index+"月的时间："+m_date);
        Date date = calendar.getTime();
        return date;
    }

    /**
     * 年
     * @param index
     * @return
     */
    public static Date getTimeByYear(Calendar calendars,int index){
        TimeZone tz = TimeZone.getTimeZone("Asia/Shanghai");
        TimeZone.setDefault(tz);
        Calendar calendar = calendars;
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        calendar.add(Calendar.YEAR,index);
        String y_date = fmt.format(calendar.getTime());
        System.out.println("当前日期"+index+"年的时间："+y_date);
        Date date = calendar.getTime();
        return date;
    }

    /**
     * 当前日期的0:0:0
     * @param date
     * @return
     */
    public static Date zero(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        Date zero = calendar.getTime();
        return zero;
    }

    /**
     * 当前日期的23:59:59
     * @param date
     * @return
     */
    public static Date lastTime(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        Date lastTime = calendar.getTime();
        return lastTime;
    }

}
